﻿using CMS6Tools.Common.Email;
using CMS6Tools.Common.TFS;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.ProcessConfiguration.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace CMS6Tools.TFSTasks.Tasks
{
    public class TfsEffortReportTask : TfsTask
    {
        public TfsEffortReportTask()
            :base()
        {
        }

        public TfsEffortReportTask(TfsTeamProjectCollection collection)
            :base(collection)
        {
        }

        protected override void ExecuteInternal(params string[] args)
        {
            var emailService = new EmailService();
            var emailTemplate = CreateEmailTemplate("DailyMatrixEmailTemplate.xslt");
            var to = AdministratorEmails.Select(a=> new MailAddress(a) ).ToArray();

            var currentIterationNode = GetCurrentIterationNode();
            if (currentIterationNode == null)
                return;

            var devMatrix = RunDailyTrackForDev(currentIterationNode);

            var emailTemplateExtraData = new TextTemplateExtraData();
            emailTemplateExtraData.Add("TodayDate", DateTime.Now.Date.ToString("yyyyMMdd"));
            emailTemplateExtraData.Add("Count", devMatrix.Count.ToString());

            int i = 1;
            foreach (var item in devMatrix)
            {
                try
                {
                    var temDic = new Dictionary<string, string>();
                    temDic.Add("Name", item.Name.ToString());
                    temDic.Add("Capacity", item.Capacity.ToString());
                    temDic.Add("Expected", item.Expected.ToString());
                    temDic.Add("TimeSpent", item.TimeSpent.ToString());
                    temDic.Add("CompletedWork", item.CompletedWork.ToString());
                    temDic.Add("TotalEstimate", item.TotalEstimate.ToString());
                    temDic.Add("ClosedTasks", item.ClosedTasks.ToString());

                    emailTemplateExtraData.AddDictionary(i, temDic);
                    i++;
                }
                catch (Exception ex)
                {
                }
            }

            //foreach (var key in totalSummary.Keys)
            //{
            //    emailTemplateExtraData.AddDictionary(i++, totalSummary[key]);
            //}

            emailService.SendEmail(to, emailTemplate, aSyncSend: false, extraData: emailTemplateExtraData);
        }

    }
}
